$(function(){
    /**
     *  @author yys
     *  Description:  权限菜单树配置
     *  @date  10:00 2018-01-15
     */
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    var code;
    /**
     *  @author yys
     *  Description:  权限菜单树配置
     *  @date  10:00 2018-01-15
     */
    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            py = $("#py").attr("checked")? "p":"",
            sy = $("#sy").attr("checked")? "s":"",
            pn = $("#pn").attr("checked")? "p":"",
            sn = $("#sn").attr("checked")? "s":"",
            type = { "Y":py + sy, "N":pn + sn};
        zTree.setting.check.chkboxType = type;
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
    }
    /**
     *  @author yys
     *  Description:  权限菜单树配置
     *  @date  10:00 2018-01-15
     */
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>"+str+"</li>");
    }
    /**
     *  @author yys
     *  Description:  初始化树菜单
     *  @date  10:00 2018-01-15
     */
    $(document).ready(function(){
        $.ajax({
            method : "POST",
            dataType : "json",
            url : "/role/selectTreeMenu",
            success : function(data){
                var  result = eval(data);
                if(result.success == true){
                    var zNodes = result.data;
                    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    setCheck();
                    $("#py").bind("change", setCheck);
                    $("#sy").bind("change", setCheck);
                    $("#pn").bind("change", setCheck);
                    $("#sn").bind("change", setCheck);
                }else{
                    layer.msg(result.msg,{icon:2,time:1000});
                }
            }
        })
    });
    /**
     *  @author yys
     *  Description:  添加权限页面提交到后台
     *  @date  10:00 2018-01-15
     */
    $("#form-role-add").validate({
        rules: {
            roleName: {
                required: true,
                minlength: 2,
                maxlength: 16
            },
            remarks: {
                required: false
            },
        },
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler:function(form){
            var roleName = $("#roleName").val();
            if($.trim(roleName) == ""){
                layer.msg("请输入合法的角色名",{icon:2,time:4000});
                return;
            }
            var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
                nodes=treeObj.getCheckedNodes(true),
                menuId="";
            for(var i=0;i<nodes.length;i++){
                menuId+=nodes[i].id + ",";
            }
            var remarks = $("#remarks").val();
            var params = { roleName:roleName , menuId:menuId , remarks:remarks};
            $.ajax({
                type: "POST",
                url: '/role/addRoleMenu',
                dataType : 'json',
                data: params,
                success: function (data) {
                    var  result = eval(data);
                    if (data.success == true) {
                        layer.msg(data.msg,{icon:1,time:2000},function(){
                            parent.location.reload();
                            //注意: 关闭打开的添加页 无法实现刷新 现修改为刷新父页面
                            // var index = parent.layer.getFrameIndex(window.name);
                            // parent.$('.btn-refresh').click();
                            // parent.layer.close(index);
                        });
                    } else if(data.success == false){
                        layer.msg(data.msg,{icon:2,time:4000});
                        return;
                    }
                }
            });
        }
    });
});